# from app.db import db
from app.db import BaseDocument, BaseEmbeddedDocument
import mongoengine as db
import datetime


class Collect(BaseEmbeddedDocument):
    """
    "ipBindDomain": True,
    "subdomain": True,
    "port": True

    Args:


    Returns:
        [type]: [description]
    """
    ipBindDomain = db.BooleanField(default=True)
    subdomain = db.BooleanField(default=True)
    port = db.BooleanField(default=True)


class TaskSettings(BaseEmbeddedDocument):
    """
    "interval": {
            "num": 1,
            "unit": "day"
        },
    "enableSitePortraitTriggerMonitor": False,
    "enable": True,
    "target": "www.cccccc.com:8080",
    "collect": {
        }
    },

    """
    interval = db.LongField(min_value=0, default=2592000000)
    enableSitePortraitTriggerMonitor = db.BooleanField(default=False)
    enable = db.BooleanField(default=False)
    target = db.StringField(default='')
    collect = db.EmbeddedDocumentField(Collect, default=Collect)
    taskId = db.StringField(default='')

    meta = {
        'strict': False
    }


class Alert(BaseEmbeddedDocument):
    enable = db.BooleanField(default=True)


class CustomTaskSettings(BaseEmbeddedDocument):
    alert = db.EmbeddedDocumentField(Alert, default=Alert)
    collect = db.EmbeddedDocumentField(Collect, default=Collect)


class Asset(BaseDocument):
    """
    "_id": {
        "$oid": "608bae0433d5a6001d9770f4"
    },
    "taskSettings": {
    "alertState": {
        "securityStatus": "unknown",
        "sendCount": 0
    },
    "alertSettings": {
        "notification": {
            "email": True,
            "sms": True,
            "wechat": True
        },
        "alertGroupIds": [
            "604d7940cefd469dfbfcb093"
        ],
        "enable": True,
        "smsDailyLimit": 0,
        "emailDailyLimit": 0
    },
    "triggerType": "schedule",
    "securityStatus": "unknown",
    "isStarted": False,
    "uid": {
        "$oid": "604d7940405c990011467f39"
    },
    "target": "www.cccccc.com:8080",
    "targetType": "domain",
    "createTime": {
        "$date": "2021-04-30T07:13:08.345Z"
    },
    "customSubdomains": [

    ],
    "jobId": {
        "$oid": "608bae0433d5a6001d9770f5"
    }


    Args:


    Returns:
        [type]: [description]
    """
    taskSettings = db.EmbeddedDocumentField(TaskSettings, default=TaskSettings)
    alertState = db.DictField(default={
        "securityStatus": "unknown",
        "sendCount": 0
    })

    alertSettings = db.DictField(default={
        "notification": {
            "email": True,
            "sms": True,
            "wechat": True
        },
        "alertGroupIds": [],
        "enable": True,
        "smsDailyLimit": 0,
        "emailDailyLimit": 0
    })
    assetType = db.StringField(default='web')
    resultId = db.ObjectIdField()
    triggerType = db.StringField(default='schedule')
    securityStatus = db.StringField(default='unknown')
    isStarted = db.BooleanField(default=False)
    uid = db.ObjectIdField()
    target = db.StringField()
    targetType = db.StringField(default='domain')
    createTime = db.DateTimeField()
    customSubdomains = []
    jobId = db.ObjectIdField()

    meta = {
        'collection': 'assets',
        'strict': False
    }

    def save(self, *args, **kwargs):
        if not self.createTime:
            self.createTime = datetime.datetime.utcnow()
        return super(Asset, self).save(*args, **kwargs)
